﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PropertyManagment.common.lang;
using PropertyManagment.entity;
using PropertyManagment.entity.dto;
using PropertyManagment.mapper;
using PropertyManagment.service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.controllers {

    /// <summary>
    /// 菜单栏控制器
    /// </summary>
    public class SysMenuController : BaseController<SysMenu> {

        /**
         * 用户当前用户的菜单和权限信息
         * @param principal
         * @return
         */

        /// <summary>
        /// 导航栏
        /// </summary>
        /// <response code="200">返回导航栏信息</response>
        /// <returns></returns>
        [HttpGet]
        [Route("/sys/menu/nav")]
        [Authorize]
        public Result nav() {
            SysUser sysUser = sysUserService.getByUsername(HttpContext.User.Identity.Name);
            // 获取权限信息
            string authorityInfo = sysUserService.getUserAuthorityInfo(sysUser.id);// ROLE_admin,ROLE_normal,sys:user:list,....
            string[] authorityInfoArray = authorityInfo.Split(",");
            // 获取导航栏信息
            List<SysMenuDto> navs = sysMenuService.getCurrentUserNav(HttpContext.User.Identity.Name);

            return Result.succ(new Dictionary<string, object> {
                ["authoritys"] = authorityInfoArray,
                ["nav"] = navs
            });
        }

        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <response code="200">返回菜单列表</response>
        /// <returns></returns>
        [HttpGet]
        [Route("/sys/menu/list")]
        [Authorize(Roles = "sys:menu:list")]
        public Result list() {
            List<SysMenu> menus = sysMenuService.tree();
            return Result.succ(menus);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="sysMenu">菜单信息</param>
        /// <response code="200">返回菜单信息</response>
        /// <returns></returns>
        [HttpPost]
        [Route("/sys/menu/save")]
        [Authorize(Roles = "sys:menu:save")]
        public Result save([FromBody] SysMenu sysMenu) {
            sysMenu.created = DateTime.Now;
            sysMenuService.add(sysMenu);
            return Result.succ(sysMenu);
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="sysMenu">菜单信息</param>
        /// <response code="200">返回菜单信息</response>
        /// <returns></returns>
        [HttpPost]
        [Route("/sys/menu/update")]
        [Authorize(Roles = "sys:menu:update")]
        public Result update([FromBody] SysMenu sysMenu) {
            sysMenu.updated = DateTime.Now;
            sysMenuService.update(sysMenu);
            // 清除所有与该菜单相关的权限缓存
            sysUserService.clearUserAuthorityInfoByMenuId(sysMenu.id);
            return Result.succ(sysMenu);
        }

        /// <summary>
        /// 获取单个菜单信息
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <response code="200">返回菜单信息</response>
        /// <response code="404">如果id为空</response>
        /// <returns></returns>
        [HttpGet]
        [Route("/sys/menu/info/{id}")]
        [Authorize(Roles = "sys:menu:list")]
        public Result info(long id) {
            return Result.succ(sysMenuService.getById(id));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <response code="200">删除成功</response>
        /// <response code="404">如果id为空</response>
        /// <returns></returns>
        [HttpPost]
        [Route("/sys/menu/delete/{id}")]
        [Authorize(Roles = "sys:menu:delete")]
        public Result delete(long id) {

            int count = sysMenuService.getAll().Where(m => m.parent_id == id).Count();

            if (count > 0) {
                return Result.fail("请先删除子菜单");
            }
            // 清除所有与该菜单相关的权限缓存
            sysUserService.clearUserAuthorityInfoByMenuId(id);
            sysMenuService.delete(id);
            // 同步删除中间关联表
            List<SysRoleMenu> roleMenus = sysRoleMenuService.getAll().Where(rm => rm.menu_id == id).ToList();
            sysRoleMenuService.deleteList(roleMenus);

            return Result.succ("删除成功");
        }
    }
}
